const API = require('../../../utils/api')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      tabCurrent:1,
      customList:[],
      userTeamCount:{},
      month:'',
      monthText:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.get_custom_list()
      this.get_user_team_data(1)
    },

    /**
     * TAB改变 - 时间选择
     */
    tabChange:function(e){
      console.log(e.currentTarget.id)
      this.setData({
        tabCurrent: e.currentTarget.id
      })
      this.get_user_team_data()
    },

    /**
     * 日期选择
     */
    dateChange:function(e){
      console.log(e.detail.value)
      this.setData({
        month: e.detail.value,
        monthText: e.detail.value.substr(5, 5),
        tabCurrent: 6
      })
      this.get_user_team_data()
    },

    /**
     * 接口 - 获取客户列表
     */
    get_custom_list:function(){
      let that = this
      const userInfo = wx.getStorageSync('userInfo')
      API.get_custom_list({
        userId: userInfo.id,
        sign: 0
      }).then( res => {
        if( res.code == 200 ){
          let customList = res.rows.map( item => {
            item.lastLoginTime = item.lastLoginTime.substr(5,5)
            return item
          })
          that.setData({
            customList: customList
          })
        }else{
          wx.showToast({
            title: '加载失败',
            icon:'none'
          })
        }
      })
    },

    /**
     * 获取推广日期数据统计
     */
    get_user_team_data:function(){
      const userInfo = wx.getStorageSync('userInfo')
      let that = this
      let form = {
        userId: userInfo.id,
      }
      if( that.data.tabCurrent == 5 ){
        form.type = ''
      }else if( that.data.month ){
        form.month = that.data.month
        form.type = ''
      }else{
        form.type = that.data.tabCurrent
      }
      form.sign = 0
      API.get_user_team_data(form).then( res => {
        if( res.code == 200 ){
          that.setData({
            userTeamCount: res.data
          })
        }
      })
    },

    /**
     * 路由 - 分享
     */
    routeShare:function(){
      wx.navigateTo({
        url: '/pages/share/share',
      })
    },

    //路由 - 成员详情
    routeMemberInfo:function(e){
        wx.navigateTo({
          url: './member-info/member-info?id=' + e.currentTarget.id,
        })
    },

    /**
     * 打电话 联系
     */
    callPhone:function(e){
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.id
      })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})